var express = require('express');
const warehouse = require('../model/warehouse');
const Account = require('../model/account');
const order = require('../model/order');
var router = express.Router();


function formateTime(){
    const thisDate = new Date()
    const year = thisDate.getFullYear();
    let month = thisDate.getMonth()+1+'';
    let day = thisDate.getDate()+'';
    if(month.length==1){
        month='0'+month;
    }
    if(day.length==1){
        day='0'+day;
    }
    const time = year+'-'+month+'-'+day;
    return time;
}

router.get('/isLogin',async function(req, res) {
    try {
        if(await (await Account.find({userId:req.user.userId})).length>0){
            res.send({
                status:0,
                userId:req.user.userId,
                message:'已登录'
            })
            return
        }else{
            res.send({
                status:1,
                message:'未找到用户'
            })
            return
        }
    } catch (error) {
        console.log(error)
        res.send({
            status:1,
            message:'未登录'
        })
    }
})

//获取仓库订单的特殊值
router.get('/specialValue',async function(req, res) {
    if(req.user.userId){
        try {
            let data1 = await warehouse.find({userId:req.user.userId,delete:'0'})
            let data2 = await order.find({userId:req.user.userId,delete:'0'})
            let wareLess = 0;
            let unfinishedOrder = 0;
            let totalWare = 0;
            let totalOrder = 0;
            let chartData1 = [[],[]];
            let chartData2 = [[],[]];
            for(let i=0;i<data1.length;i++){
                if(data1[i].nums<3){
                    wareLess++;
                }
                if(data1[i].date == formateTime()){
                    totalWare++;
                }
                chartData1[0].push(data1[i].nums)
                chartData1[1].push(data1[i].name)
            }
            for(let i=0;i<data2.length;i++){
                if(data2[i].date == formateTime()){
                    totalOrder++;
                }
                if(data2[i].finish == '0'){
                    unfinishedOrder++;
                }
                if(chartData2[1].length<=7){
                    let index = chartData2[1].indexOf(data2[i].date)
                    if(index<0){
                        chartData2[0].push(data2[i].price)
                        chartData2[1].push(data2[i].date)
                    }else{
                        let price = Number(chartData2[0][index])+Number(data2[i].price)
                        chartData2[0][index] = price+''
                    }
                }
            }
            res.send({
                status:0,
                data:{
                    wareLess,
                    unfinishedOrder,
                    totalOrder,
                    totalWare
                },
                chartData1,
                chartData2,
                message:'获取成功'
            })
            return
        } catch (error) {
            res.send({
                status:1,
                message:'error'
            })
            return
        }
        
    }else{
        res.send({
            status:1,
            message:'error'
        })
        return
    }
})


router.get('/warehouse',async function(req, res) {
    if(req.user.userId){
        try {
            let data = await warehouse.find({userId:req.user.userId,delete:'0'})
            res.send({
                status:0,
                data,
                message:'获取成功'
            })
            return
        } catch (error) {
            res.send({
                status:1,
                message:error
            })
            return
        }
        
    }else{
        res.send({
            status:1,
            message:'error'
        })
        return
    }
})

router.post('/warehouse',async function(req, res) {
    let newdata = {uuid:'',date:'',name:'',nums:'',price:'',totalPrice:'',sulipper:'',phone:''};
    Object.keys(req.body).forEach((key)=>{
        newdata[key] = req.body[key]
    })
    newdata.delete='0'
    newdata.userId = req.user.userId;
    await warehouse.create(newdata,function(err){
        if(err){
            res.send({
                status:1,
                message:'添加失败'
            });
        }else{
            res.send({
                status:0,
                message:'添加成功'
            });
        }
    })
})


router.post('/changeWarehouse',async function(req, res) {
    let newdata = {uuid:'',date:'',name:'',nums:'',price:'',totalPrice:'',sulipper:'',phone:''};
    Object.keys(req.body).forEach((key)=>{
        newdata[key] = req.body[key]
    })
    try {
        let data = await warehouse.updateOne({uuid:req.body.uuid},newdata)
        res.send({
            status:0,
            data,
            message:'修改成功'
        })
        return
    } catch (error) {
        res.send({
            status:1,
            message:error
        })
        return
    }
})

router.get('/deleteWarehouse',async function(req, res) {
    try {
        let data = await warehouse.updateOne({uuid:req.query.uuid},{delete:'1'})
        res.send({
            status:0,
            data,
            message:'删除成功'
        })
        return
    } catch (error) {
        res.send({
            status:1,
            message:error
        })
        return
    }
})

router.get('/order',async function(req, res) {
    if(req.user.userId){
        try {
            let data = await order.find({userId:req.user.userId,delete:0})
            res.send({
                status:0,
                data,
                message:'获取成功'
            })
            return
        } catch (error) {
            res.send({
                status:1,
                message:error
            })
            return
        }
        
    }else{
        console.log(req.query.userId)
        res.send({
            status:1,
            message:'error'
        })
        return
    }
})

router.post('/order',async function(req, res) {
    let newdata = {uuid:'',date:'',name:'',price:'',phone:'',address:'',finish:''};
    console.log(req.body)
    Object.keys(req.body).forEach((key)=>{
        newdata[key] = req.body[key]
    })
    newdata.delete='0'
    newdata.userId = req.user.userId;
    await order.create(newdata,function(err){
        if(err){
            res.send({
                status:1,
                message:'添加失败'
            });
        }else{
            res.send({
                status:0,
                message:'添加成功'
            });
        }
    })
})

router.post('/changeOrder',async function(req, res) {
    let newdata = {uuid:'',date:'',name:'',price:'',phone:'',address:'',finish:''};
    Object.keys(req.body).forEach((key)=>{
        newdata[key] = req.body[key]
    })
    try {
        let data = await order.updateOne({uuid:req.body.uuid},newdata)
        res.send({
            status:0,
            data,
            message:'修改成功'
        })
        return
    } catch (error) {
        res.send({
            status:1,
            message:error
        })
        return
    }
})


router.get('/deleteOrder',async function(req, res) {
    try {
        let data = await order.updateOne({uuid:req.query.uuid},{delete:'1'})
        res.send({
            status:0,
            data,
            message:'删除成功'
        })
        return
    } catch (error) {
        res.send({
            status:1,
            message:'删除失败'
        })
        return
    }
})
module.exports = router;

